from PIL import Image
import os

def images_to_gif(image_folder, output_gif, duration=200):
    # 获取文件夹中的所有图片文件
    images = [img for img in os.listdir(image_folder) if img.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
    images.sort()  # 按文件名排序

    # 打开图片并存储在列表中
    frames = []
    for image in images:
        img_path = os.path.join(image_folder, image)
        frame = Image.open(img_path)
        frames.append(frame)

    # 保存为GIF
    frames[0].save(output_gif, save_all=True, append_images=frames[1:], duration=duration, loop=0)

    print(f"GIF saved as {output_gif}")

# 使用示例
image_folder = 'images'  # 替换为你的图片文件夹路径
output_gif = 'output.gif'  # 输出的GIF文件名
images_to_gif(image_folder, output_gif, duration=200)